/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserServlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import UserManager.*;

/**
 *
 * @author dizonfarleyjr
 */
@WebServlet(name = "loginServlet", urlPatterns = {"/loginServlet"})
public class loginServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        String ErrorMessage = "Your Account has been locked by an Administrator.";
        
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet loginServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet loginServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
            
            String username = request.getParameter("loginUsername");
            String password = request.getParameter("loginPassword");
            
            int checkActive = new UserManager().checkIsActive(username);
            
            String checkType = new UserManager().checkUserType(username);
            
         //   System.out.println(checkActive);
           // if(checkActive > 0){
            int check = new UserManager().logIn(username, password);
            if(check == 4){
                session.invalidate();
                session = request.getSession(true);
                User UserDetails = new User();
                UserDetails.setUsername(username);
                new UserManager().viewUser(UserDetails); //get user details using viewUser function
                session.setAttribute("UserData", UserDetails);
                CreditCard CardDetails = new CreditCard();
                CardDetails.setCreditNumber(UserDetails.getCreditNumber());
                new CreditCardManager().viewCard(CardDetails);
                session.setAttribute("CardData", CardDetails);
                session.setAttribute("loggedName", username);
                session.setAttribute("loggedUserType", checkType);
                session.setMaxInactiveInterval(600);
                
                if("Customer".equals(checkType)){
                    response.sendRedirect("customer.jsp");
                }
                else if("Administrator".equals(checkType))
                    response.sendRedirect("admin.jsp");
                else if("Product Manager - Book".equals(checkType))
                    response.sendRedirect("bookProductManager.jsp");
                else if("Product Manager - Magazine".equals(checkType))
                    response.sendRedirect("magazineProductManager.jsp");
                else if("Product Manager - AudioCD".equals(checkType))
                    response.sendRedirect("audioCDProductManager.jsp");
                else if("Product Manager - DVD".equals(checkType))
                    response.sendRedirect("dvdProductManager.jsp");
                else if("Accounting Manager".equals(checkType))
                    response.sendRedirect("AccountingManager.jsp");
            }
            else if(check == 3 || check == 5){//if user or password is incorrect
                response.sendRedirect("loginFailed.jsp");
            }
            else if(check == 2){//if product / accounting manager account expired
                response.sendRedirect("accountExpired.jsp");
            }
            else if(check == 1){//if there's 5 failed password tries
                response.sendRedirect("accountlocked.jsp");
            }
            else if(check == 7){//if locked by an administrator
                response.sendRedirect("adminlocked.jsp");
            }
            
         //   else if(check == true && checkActive == 0){
         //   out.print("<html><head>");
         //   out.print("<script type=\"text/javascript\">alert(" + ErrorMessage + ");</script>");
         //   out.print("</head><body></body></html>");
           // }
          //  else if (checkActive <= 0)
               // response.sendRedirect("accountlocked.jsp");
            //else
            //    response.sendRedirect("login.jsp");
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
